//
//  Kumquat.m
//  Return Of The Lemi
//
//  Created by Paul Vishayanuroj on 11/14/09.
//  Copyright 2009 Paul Vishayanuroj. All rights reserved.
//

#import "Kumquat.h"


@implementation Kumquat

@synthesize counter;

// NOT USED CONSTRUCTORS /////
+ (id) kumquatWithPosition:(CGPoint)pos space:(cpSpace *)space
{
	return [[[self alloc] initWithPosition:pos space:space] autorelease];
}

- (id) initWithPosition:(CGPoint)pos space:(cpSpace *)space
{
	if ((self = [super init]))
	{
		sprite = [[Sprite spriteWithFile:@"Kumquat.png"] retain];
		[sprite setScale:CONST_KUMQUAT_SCALE];
		[sprite setPosition:pos];
		
		counter = CONST_KUMQUAT_LIFETIME;
		
		// Create the kumquat body with given mass and moment of inertia
		body = cpBodyNew(CONST_KUMQUAT_MASS, cpMomentForCircle(CONST_KUMQUAT_MASS, 0.0f, CONST_KUMQUAT_RADIUS, cpvzero));
		
		// Set the initial position
		body->p = cpv(pos.x, pos.y);
		
		// Give it an angle
		cpBodySetAngle(body, 1.5f);
		
		// Give it an initial velocity
		body->v = cpv(CONST_KUMQUAT_VELOCITY, CONST_KUMQUAT_VELOCITY);
		
		// Set an angular velocity (how fast it's rotating)
		body->w = CONST_KUMQUAT_ANG_VEL;
		
		// Add the body to space
		cpSpaceAddBody(space, body);
		
		// Create our shape associated with the ball's body
		shape = cpCircleShapeNew(body, CONST_KUMQUAT_RADIUS, cpvzero);
		
		shape->e = 0.1f; // Elasticity
		
		shape->u = 1.0f; // Friction
		
		shape->data = self; // Associate with kumquat object
		
		shape->collision_type = 2; // Collisions are grouped by types	
		
		// Add the shape to our space
		cpSpaceAddShape(space, shape);			
	}
	return self;
}

+ (id) kumquatFromDrag:(CGPoint)start stop:(CGPoint)stop pos:(CGPoint)pos space:(cpSpace *)space
{
	return [[[self alloc] initFromDrag:start stop:stop pos:pos space:space] autorelease];
}
			
- (id) initFromDrag:(CGPoint)start stop:(CGPoint)stop pos:(CGPoint)pos space:(cpSpace *)space
{
	if ((self = [super init]))
	{
		sprite = [[Sprite spriteWithFile:@"Kumquat.png"] retain];
		[sprite setScale:CONST_KUMQUAT_SCALE];
		[sprite setPosition:pos];
		
		counter = CONST_KUMQUAT_LIFETIME;
		
		// Calculate initial velocity based on waypoints
		// Determine slope first
		CGFloat m = (float)(stop.y - start.y) / (float)(stop.x - start.x);
		CGFloat theta = atan(m);
		
		// If we are throwing to the left
		if (stop.x < start.x) 
		{
			theta += M_PI;
			pos.x += CONST_KUMQUAT_LAUNCH_BACKWARD_X;
			pos.y += CONST_KUMQUAT_LAUNCH_BACKWARD_Y;
		}
		else
		{
			pos.x += CONST_KUMQUAT_LAUNCH_FORWARD_X;
			pos.y += CONST_KUMQUAT_LAUNCH_FORWARD_Y;
		}
		
		// Calculate x and y components of velocity based on drag angle
		CGFloat xVel = CONST_KUMQUAT_VELOCITY * cos(theta);
		CGFloat yVel = CONST_KUMQUAT_VELOCITY * sin(theta);
		
		// Create the kumquat body with given mass and moment of inertia
		body = cpBodyNew(CONST_KUMQUAT_MASS, cpMomentForCircle(CONST_KUMQUAT_MASS, 0.0f, CONST_KUMQUAT_RADIUS, cpvzero));		
		
		// Set the initial position
		body->p = cpv(pos.x, pos.y );
		
		// Set its velocity
		body->v = cpv(xVel, yVel);
		
		// Give it an angle
		cpBodySetAngle(body, 1.5f);		
		
		// Set an angular velocity (how fast it's rotating)
		body->w = CONST_KUMQUAT_ANG_VEL;
		
		// Add the body to space
		cpSpaceAddBody(space, body);
		
		// Create our shape associated with the ball's body
		shape = cpCircleShapeNew(body, CONST_KUMQUAT_RADIUS, cpvzero);
		
		shape->e = 0.5f; // Elasticity
		
		shape->u = 1.0f; // Friction
		
		shape->data = self; // Associate with kumquat sprite
		
		shape->collision_type = cTagPlayerMunitions; // Collisions are grouped by types	
		
		// Add the shape to our space
		cpSpaceAddShape(space, shape);			
	}
	return self;	
}

- (void) dealloc
{
	[sprite dealloc];
	[super dealloc];
}

@end
